//
//  LLPUserDaults.m
//  LLP
//
//  Created by Shiyu pan on 12-10-12.
//  Copyright (c) 2012年 Porcellio. All rights reserved.
//

#import "LLPUserDefaults.h"
#import "LLPImageFileCache.h"

@implementation LLPUserDafaults

+ (void)setValue:(id)value forKey:(NSString *)key{
    [[NSUserDefaults standardUserDefaults]setValue:value forKey:key];
    [[NSUserDefaults standardUserDefaults]synchronize];
}
+ (id)valueForKey:(NSString *)key{
    return [[NSUserDefaults standardUserDefaults]valueForKey:key];
}
+ (void)clearData{
    [NSUserDefaults resetStandardUserDefaults];
}

@end

//#pragma mark - 用户信息
#define kUserName   @"kUserName"
@implementation LLPUserDafaults (userInfo)

+ (void)saveUserName:(NSString*)tmpName{
    [LLPUserDafaults setValue:tmpName forKey:kUserName];
}
+ (NSString*)userName{
    return [LLPUserDafaults valueForKey:kUserName];
}

@end

#define kImgDataList    @"kImgDataList"
@implementation LLPUserDafaults (imageList)

+ (void)saveImageDataDictionary:(NSDictionary*)tmpImgDataDictionary{
    [LLPUserDafaults setValue:tmpImgDataDictionary forKey:kImgDataList];
}
+ (NSDictionary*)imgDataDictionary{

    BOOL isFirstVisit = [LLPUserDafaults isFirstVisit];
    
    if (isFirstVisit) {
        [LLPUserDafaults clearImageDataDictory];
        [LLPUserDafaults invalidIsFirstVisit];
    }
    
    NSDictionary* tmpDic = [LLPUserDafaults valueForKey:kImgDataList];
    if (tmpDic == nil) {
        tmpDic = [NSDictionary dictionary];
    }
    return tmpDic;
}

+ (void)clearImageDataDictory{
    [LLPUserDafaults setValue:nil forKey:kImgDataList];
}

@end


#define kIsFirstVisit  @"kIsFirstVisit"
@implementation LLPUserDafaults (setting)

+ (BOOL)isFirstVisit{
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString* dataPath = [[paths objectAtIndex:0]stringByAppendingPathComponent:@"data.plist"];
    NSDictionary* nativeDicData = [NSDictionary dictionaryWithContentsOfFile:dataPath];
    if ([nativeDicData valueForKey:kIsFirstVisit]) {
        return [[nativeDicData valueForKey:kIsFirstVisit]boolValue];
    }
    return YES;
}
+ (void)invalidIsFirstVisit{
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString* dataPath = [[paths objectAtIndex:0]stringByAppendingPathComponent:@"data.plist"];
    NSMutableDictionary* nativeDicData = [NSMutableDictionary dictionaryWithDictionary:[NSDictionary dictionaryWithContentsOfFile:dataPath]];
    [nativeDicData setValue:[NSNumber numberWithBool:NO] forKey:kIsFirstVisit];
    [nativeDicData writeToFile:dataPath atomically:YES];
}

@end

